package com.lazily.list_;


import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @Author: GZQ
 * @date: 2025/4/19 22:13
 * @description: List 接口实现类的练习
 * @version: 1.0
 */

public class ListExercise02 {
	// 程序入口
	@SuppressWarnings({"all"})
	public static void main(String[] args) {

		List list = new ArrayList();
		// List list = new Vector();
		// List list = new LinkedList();

		list.add(new Book("三国", "罗贯中", 30.03));
		list.add(new Book("西游记", "吴承恩", 33.61));
		list.add(new Book("天龙", "金庸", 50.65));
		list.add(new Book("水浒传", "施耐庵", 90.32));

		// 如何对集合进行排序

		// 遍历
		for (Object o : list) {
			System.out.println(o);
		}

		// 冒泡排序
		sort(list);
		System.out.println("====排序后=====");
		for (Object o : list) {
			System.out.println(o);
		}

	}

	// 静态方法
	// 价格要求是从小到大
	public static void sort(List list) {

		int listSize = list.size();
		for (int i = 0; i < listSize - 1; i++) {
			for (int j = 0; j < listSize - i - 1; j++) {
				// 取出对象Book
				Book book1 = (Book) list.get(j);
				Book book2 = (Book) list.get(j + 1);
				if (book1.getPrice() > book2.getPrice()) { // 交换
					list.set(j, book2);
					list.set(j + 1, book1);
				}
			}

		}
	}
}


class Book {
	private String name;
	private String author;
	private double price;

	public Book(String name, String author, double price) {
		this.name = name;
		this.author = author;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "名称：" + name + "\t\t" +
				"价格：" + price + "\t\t" +
				"作者: " + author;
	}
}